package com.yangfei.mystudy1020.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.widget.Toast;

import com.yangfei.mystudy1020.OGL20.Renderer.FirstOGLRenderer;
import com.yangfei.mystudy1020.OGL20.Renderer.FirstShaderOGLRenderer;
import com.yangfei.mystudy1020.OGL20.Renderer.SecondShaderOGLRenderer;

public class OGL20Activity extends Activity {

    GLSurfaceView mGLSurfaceView;
    private boolean rendererSet = false;
    boolean isOGL20 = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_ogl20);

        initOGL();
    }

    private void initOGL(){
        mGLSurfaceView = new GLSurfaceView(this);

        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        if(activityManager!=null){
            ConfigurationInfo config = activityManager.getDeviceConfigurationInfo();
            if(config.reqGlEsVersion >= 0x20000){
                isOGL20 = true;
                Toast.makeText(this,"OpenGL ES 2.0 is OK",Toast.LENGTH_LONG).show();
            }else{
                isOGL20 = false;
                Toast.makeText(this,"OpenGL ES 2.0 is not OK",Toast.LENGTH_LONG).show();
            }
        }

        if(isOGL20){
            mGLSurfaceView.setEGLContextClientVersion(2);

            mGLSurfaceView.setRenderer(new SecondShaderOGLRenderer(this));
        }else{

        }

        setContentView(mGLSurfaceView);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(rendererSet){
            mGLSurfaceView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if(rendererSet){
            mGLSurfaceView.onPause();
        }
    }
}
